package ren.nearby.http.base;

import android.annotation.TargetApi;
import android.content.Intent;
import android.graphics.Typeface;
import android.os.Build;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.ActivityOptionsCompat;
import android.support.v4.util.Pair;
import android.support.v7.widget.Toolbar;
import android.text.TextUtils;
import android.transition.Slide;
import android.transition.Transition;
import android.transition.TransitionInflater;
import android.transition.Visibility;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.orhanobut.logger.Logger;

import dagger.android.AndroidInjection;
import ren.nearby.http.R;

//import butterknife.ButterKnife;
//import butterknife.Unbinder;


/**
 * Created by Administrator on 2016/4/19.
 */
public class BaseActivity extends BaseTitleActivity {

    /**
     * 控件注解绑定
     */
//    private Unbinder unbinder;

    /**
     * 透明侧滑背景层
     */
    private ImageView ivShadow;



    private View.OnClickListener mOnEmptyListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
//            Toast.makeText(getActivity(), "Do something when empty_empty", Toast.LENGTH_SHORT).show();
        }
    };

    private View.OnClickListener mOnErrorListener = new View.OnClickListener() {
        @Override
        public void onClick(View v) {
//            Toast.makeText(getActivity(), "Do something when empty_error", Toast.LENGTH_SHORT).show();
        }
    };

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        AndroidInjection.inject(this);
        super.onCreate(savedInstanceState);
        onIntent();
        //activity传递数据Intent
        if (isStateLayout()) {
//
        } else if (getContentView() != 0) {
//            Log.e(" onCreate - else ");
            setContentView(getContentView());
        }

        //操作标题栏
        initViewHeader();
        //android沉浸式状态栏实现
        initBarTint();
        initKnife();
        initView();

    }

    @Override
    public void initView() {
//        Logger.e("initView");
    }

    /**
     * 获取传递数据
     */
    public void onIntent() {
//        Logger.e("onIntent");
    }

    public Toolbar id_toolbar;

    protected void initViewHeader() {
        id_toolbar = (Toolbar) findViewById(R.id.toolbar_id);
        if (id_toolbar != null) {
            id_toolbar.setTitle("");
            setSupportActionBar(id_toolbar);
            id_toolbar.setNavigationOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    onClickLeft(v);
                }
            });
            //标题栏信息
            TextView index_tv_title = (TextView) findViewById(R.id.toolbar_tv_title);
            RelativeLayout relative = (RelativeLayout) findViewById(R.id.relative);
            RelativeLayout bgc = findViewById(R.id.bgc);
            Toolbar.LayoutParams lp = new Toolbar
                    .LayoutParams(Toolbar.LayoutParams.MATCH_PARENT, Toolbar.LayoutParams.WRAP_CONTENT);
            if (showTitlePosition() == 0) {
                relative.setGravity(Gravity.LEFT);
                relative.setLayoutParams(lp);
            }
            index_tv_title.setText(TextUtils.isEmpty(showTitle()) ? "" : showTitle());
            if (isTTF()) {//字体类型
                TextView toolbar_tv_title_bttom = (TextView) findViewById(R.id.toolbar_tv_title_bttom);
                toolbar_tv_title_bttom.setVisibility(View.VISIBLE);
                Typeface face2 = Typeface.createFromAsset(getAssets(), "fonts/hwcy.ttf");
                toolbar_tv_title_bttom.setTypeface(face2);
                Typeface face = Typeface.createFromAsset(getAssets(), "fonts/hwcy.ttf");
                index_tv_title.setTypeface(face);
            }

            //是否显示左边图标
            if (isShowLeftIcon()) {
                getSupportActionBar().setDisplayHomeAsUpEnabled(true);
                id_toolbar.setNavigationIcon(setLeftIcon());
            } else {
                getSupportActionBar().setDisplayHomeAsUpEnabled(false);
            }
            //是否显示右边图标
            if (isShowRightIcon()) {
                ImageView index_iv_right = (ImageView) findViewById(R.id.toolbar_iv_right);
                index_iv_right.setVisibility(View.VISIBLE);
                index_iv_right.setBackgroundResource(setRightIcon());
                index_iv_right.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        onClickRight(v);

                    }
                });
            }
            if (setBGC() != 0) {
                bgc.setBackgroundResource(setBGC());
            } else {
                bgc.setBackgroundResource(getBGC());
            }

        }
    }

    /**
     * 标题栏显示的位置 0左边 1 中间
     *
     * @return
     */
    public int showTitlePosition() {
        return 1;
    }

    /**
     * 处理侧滑层
     *
     * @return
     */
    private View getContainer()  {
        return null;
    }

    /**
     * 处理那些activity可以侧滑
     *
     * @param layoutResID
     */
    @Override
    public void setContentView(int layoutResID) {
        if (isStateLayout()) {
            Logger.e(" setContentView - if ");
            super.setContentView(getContainer());
            View view = LayoutInflater.from(this).inflate(layoutResID, null);
            view.setBackgroundColor(getResources().getColor(R.color.window_background));
        } else {
//            Logger.e(" setContentView - else ");
            super.setContentView(layoutResID);
        }
    }

    @Override
    protected void onStart() {
        super.onStart();
//        Logger.e("onStart");
    }

    @Override
    protected void onResume() {
        super.onResume();
        Logger.e("onResume");
    }

    @Override
    protected void onPause() {
        super.onPause();
        Logger.e("onPause");
    }

    @Override
    protected void onStop() {
        super.onStop();
        Logger.e("onStop");
    }


    @Override
    protected void onDestroy() {
        super.onDestroy();
        Logger.e("onDestroy");
//        unbinder.unbind();

    }

    public void initBarTint() {

    }

    @TargetApi(19)
    private void setTranslucentStatus(boolean on) {
        Window win = getWindow();
        WindowManager.LayoutParams winParams = win.getAttributes();
        final int bits = WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS;
        if (on) {
            winParams.flags |= bits;
        } else {
            winParams.flags &= ~bits;
        }
        win.setAttributes(winParams);
    }


    @Override
    public IPresenter getPresenter() {
        return null;
    }

    @Override
    public int getContentView() {
        Logger.e("father getContentView");
        return 0;
    }


    @Override
    public void initKnife() {
//        Logger.e("initKnife");
//        unbinder = ButterKnife.bind(this);//初始化在oncreate()方法里面
    }


    @Override
    public BaseActivity getActivity() {
        return this;
    }



    static final int TYPE_PROGRAMMATICALLY = 0;
    private int type = 0;


    /**
     * 进入界面动画效果
     */
    public void setupWindowAnimations() {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            initWindowAnimations();
        }

    }

    @TargetApi(21)
    public void initWindowAnimations() {
        Transition transition;
        if (type == TYPE_PROGRAMMATICALLY) {
            transition = buildEnterTransition();
        } else {
            transition = TransitionInflater.from(this).inflateTransition(R.transition.slide_from_bottom);
        }
        getWindow().setEnterTransition(transition);
    }

    /**
     * 代码动画方式
     *
     * @return
     */
    @TargetApi(21)
    public Visibility buildEnterTransition() {
        Slide enterTransition = new Slide();
        enterTransition.setDuration(getResources().getInteger(R.integer.anim_duration_long));
        enterTransition.setSlideEdge(Gravity.RIGHT);
        return enterTransition;
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
    }

    @Override
    public void finish() {
        super.finish();
        overridePendingTransitionExit();
    }

    @Override
    public void startActivity(Intent intent) {
        super.startActivity(intent);
        overridePendingTransitionEnter();
    }

    /**
     * Overrides the pending Activity transition by performing the "Enter" animation.
     */
    protected void overridePendingTransitionEnter() {
        overridePendingTransition(R.anim.slide_form_right, R.anim.slide_to_left);
    }

    /**
     * Overrides the pending Activity transition by performing the "Exit" animation.
     */
    protected void overridePendingTransitionExit() {
        overridePendingTransition(R.anim.slide_form_left, R.anim.slide_to_right);
    }

}
